package com.arofoxmsn.utils;

import java.util.Map;

public class ParametersTool {
	
	public static void testNullObject(Object...objects){
		for(Object o : objects){
			if(o == null){
				throw new IllegalArgumentException();
			}
		}
	}
	
	public static String testMandatoryFields(Map<String,Object> objects){
		String wrongParam = null;
		for(String key : objects.keySet()){
			try{
				testNullObject(objects.get(key));
			}catch(IllegalArgumentException e){
				if(wrongParam == null){
					wrongParam = new String();
				}
				wrongParam += key+"\n";
			}
		}
		return wrongParam;
		
	}
	
	public static boolean testNullObjectBoolean(Object...objects){
		try{
			testNullObject(objects);
			return true;
		}catch(IllegalArgumentException e){
			return false;
		}
	}
	
	public static void testNullString(String ...args) throws IllegalArgumentException{
		testNullObject((Object[])args);
		for(String one : args){
			if(one.equals(""))
				throw new IllegalArgumentException();
		}
	}
	
	public static boolean testNullStringBoolean(String ...args){
		try{
			testNullString(args);
			return true;
		}catch(IllegalArgumentException e){
			return false;
		}
	}
	/**
	 * this method is in charge of checking null objets
	 * @param args
	 */
	public static void checkNullObject(Object ...args){
		for(Object one : args){
			if(one == null)
				throw new IllegalArgumentException();
		}
	}
	/**
	 * this method is in charge of checking objects. If it finds
	 * one of them that is null it returns true;
	 * @param args
	 * @return <b>true</b> when one object is null
	 */
	public static boolean checkNullObjectBoolean(Object ...args){
		try{
			checkNullObject(args);
			return true;
		}catch(IllegalArgumentException e){
			return false;
		}
	}
	
	public static void checkEmptyString(String...args){
		try{
			checkNullObject(args);
			for(String one : args){
				if(one.equals(""))
					throw new IllegalArgumentException();
			}
		}catch(IllegalArgumentException e){
			throw new IllegalArgumentException();
		}
	}
	
	public static boolean checkEmptyStringBolean(String...args){
		try{
			checkEmptyString(args);
			return true;
		}catch(IllegalArgumentException e){
			return false;
		}
	}
	
	public static String kilometersToString(double total){
		String result = new String();
		int kilometers = (int)total;
		double metters = total - kilometers;
		if(total > 1){
			if(kilometers < 2)
				result =  "one kilometer";
			result = kilometers+" kilometers";
			return result + " and "+ getMeters(metters);
		}
		return getMeters(metters);
	}
	
//	private String getKilometersToString(double km){
//		int kilometers = (int)km;
//		if(kilometers < 2)
//			return "one kilometer";
//		return kilometers+" kilometers";
//	}
	
	private static String getMeters(double metters){
		metters *= 1000;
		if(metters < 2){
			return "one meter";
		}
		return (int)metters+" meters";
	}
}
